<?php
error_reporting(0);
require_once 'model/api/Taoapi.php';
require_once 'model/function.php';
require_once 'config.php';


$page = !($_GET['page'])?'0':intval($_GET['page']);
$nick = empty($_GET['nick'])? '柠檬绿茶':$_GET['nick'];
$sort=$_GET['sort'];
  if($sort==''){$sort='volume';}
  
$tplFile="shop.html";
$tplCache=$paeg.$nick.$sort;
$smar=new MySmarty($cache,$cachetime,$moban);
$smarty=$smar->get_smarty();
if(!$smarty->is_cached($tplFile,$tplCache)){
	$Taoapi_Config = Taoapi_Config::Init();
	$Taoapi_Config->setCharset('UTF-8');	$Taoapi_Config->setAppKey($app);
	$Taoapi = new Taoapi;
	$Taoapi->method = 'taobao.shop.get';
	$Taoapi->fields = 'cid,sid,nick,title,pic_path,created,desc';
	$Taoapi->nick = $nick;
	$result2 = $Taoapi->Send('get','xml')->getArrayData();
	$shopinfo = $result2['shop'];

	$Taoapi1 = new Taoapi;
	$Taoapi1->method = 'taobao.user.get';
	$Taoapi1->fields = 'seller_credit,location,user_id';
	$Taoapi1->nick = $nick;
	$result2 = $Taoapi1->Send('get','xml')->getArrayData();
	$userinfo = $result2['user'];

	$Taoapi2 = new Taoapi;
	$Taoapi2->method = 'taobao.items.get';
	$Taoapi2->fields = 'num_iid';
	$Taoapi2->page_no = $page;
	$Taoapi2->sort = $sort;
	$Taoapi2->page_size = '20';
	$Taoapi2->nicks = $nick;
	$result1 = $Taoapi2->Send('get','xml')->getArrayData();
	$totalResults=$result1['total_results'];  
	$num_iid=$result1['items']['item'];

	if($num_iid){
		foreach($num_iid as $row){
			$numiid.=$row['num_iid'].',';
		}
	}

	$Taoapi3 = new Taoapi;
	$Taoapi3->method = 'taobao.taobaoke.items.convert';
	$Taoapi3->fields = 'num_iid,title,pic_url,price,click_url,volume,shop_click_url';
	$Taoapi3->nick = $usernick;
	$Taoapi3->num_iids = $numiid;
	$result = $Taoapi3->Send('get','xml')->getArrayData();
	$taobaokeItem=$result['taobaoke_items']['taobaoke_item'];
	foreach ($taobaokeItem as $row) { 
		$row["click_url"]=base64_encode($row["click_url"]);
		$row["title"]=strip_tags($row["title"]);
		$row["pic_url"]=base64_encode($row["pic_url"]);
		$proItems[]=$row;
	}

	$con=file_get("http://favorite.taobao.com/collect_item_relation---".$shopinfo['sid']."-0-.htm",NULL,NULL,0,15*1024);
	$liststart=iconv("utf-8","gb2312",'收藏人气：<STRONG> ');
	$listend=('<\/STRONG>');
	preg_match("/$liststart(.*)$listend/Uis",$con,$content);
	
	if(empty($content)){
		$collect="∞";
	}else{
		$collect=$content[1];
	}
	if($userinfo['seller_credit']['total_num']){
		$perfect=(number_format($userinfo['seller_credit']['good_num']/$userinfo['seller_credit']['total_num'],4)*100)."%";
	}
	if(is_array($shopinfo['desc'])){$shangcheng=1;}else{$shangcheng=0;}
	$conn=mysql_connect($servername,$dbusername,$dbpsw) or die ("数据库连接失败");
	mysql_select_db($dbname,$conn);
	mysql_query("set names utf8");   
	$exist=mysql_query("select s_id from ".$prefix."shoplist where s_nick='".$nick."'",$conn);
	$a=mysql_fetch_array($exist);
	
	if(!$a['s_id']){
		if(!empty($taobaokeItem[0]['shop_click_url'])){
			$sql="insert into ".$prefix."shoplist(s_collect,s_cid,s_shangcheng,s_clickurl,s_title,s_location,s_level,s_perfect,s_nick) values('".$collect."','".$shopinfo['cid']."','".$shangcheng."','".$taobaokeItem[0]['shop_click_url']."','".$shopinfo['title']."','".$userinfo['location']['state'].$userinfo['location']['city']."','".$userinfo['seller_credit']['level']."','".(float)$perfect."','".$nick."')";
		}else{
			$sql="";
		}
		
	}else{
		if(!$taobaokeItem[0]['shop_click_url']){
			$sql="delete from ".$prefix."shoplist where s_nick='".$nick."'";
		}else{
			$sql="update ".$prefix."shoplist set s_collect='".$collect."',s_cid='".$shopinfo['cid']."',s_shangcheng='".$shangcheng."',s_clickurl='".$taobaokeItem[0]['shop_click_url']."',s_title='".$shopinfo['title']."',s_location='".$userinfo['location']['state'].$userinfo['location']['city']."',s_level='".$userinfo['seller_credit']['level']."',s_perfect='".(float)$perfect."' where s_nick='".$nick."'";
		}
	}
	
	mysql_query($sql);
	$thistitle=$shopinfo['title'];
	$prefixurl="huangguandianpu";
	
	include("head.php");
	include_once("demo.php");
	
	
	$pat = "/<(\/?)(script|i?frame|style|html|body|title|link|a|meta|\?|\%)([^>]*?)>/isU";
	$abc=preg_replace($pat,"",$shopinfo['desc']);
	
	require_once("pages.php");    
	  $page_size=20;   
	  $nums=$totalResults;     
	  $sub_pages=10;   
	  $pageCurrent=$_GET["page"];     
	  $subPages=new SubPages($page_size,$nums,$pageCurrent,$sub_pages,$website."/shop".$ljoin.$nick.$ljoin.$lpage,2,$rewrite_postfix);
	  $pagestr=$subPages->getstr();
	  
	include("foot.php");
	
	//head
	$smarty->assign("thistitle",$thistitle);
	$smarty->assign("q",$q);
	$smarty->assign("result2",$result2);
	$smarty->assign("webname",$webname);
	$smarty->assign("postfix",$postfix);
	$smarty->assign("description",$description);
	$smarty->assign("keywords",$keywords);
	$smarty->assign("website",$website);
	$smarty->assign("searchkeys",$searchkeys);
	$smarty->assign("newshops",$newshops);
	$smarty->assign("toubiao",$show_topnav);
$smarty->assign("moban",$moban);
$smarty->assign("thispid",$thispid);

	
	//category
	$smarty->assign("prefixurl",$prefixurl);
	$smarty->assign("lcat",$lcat);
	$smarty->assign("lpro",$lpro);
	$smarty->assign("lpage",$lpage);
	$smarty->assign("rewrite_postfix",$rewrite_postfix);
	$smarty->assign("ljoin",$ljoin);
	//foot
	$smarty->assign("links",$links);
	$smarty->assign("QQ",$QQ);
	$smarty->assign("ICP",$ICP);
	$smarty->assign("tongji",$tongji);
$smarty->assign("jiathis",$jiathis);
	//demo
	$smarty->assign("a",$digg);
	$smarty->assign("up",$up);
	$smarty->assign("down",$down);
	$smarty->assign("up_width",$up_width);
	$smarty->assign("down_width",$down_width);
	$smarty->assign("up_percent",$up_percent);
	$smarty->assign("down_percent",$down_percent);
	//shop
	$smarty->assign("proItems",$proItems);
	$smarty->assign("subPages",$pagestr);
	$smarty->assign("page",$pageCurrent);
	$smarty->assign("abc",$abc);
	$smarty->assign("userinfo",$userinfo);
	$smarty->assign("shopinfo",$shopinfo);
	$smarty->assign("taobaokeItem",$taobaokeItem);
	$smarty->assign("perfect",$perfect);
	$smarty->assign("collect",$collect);
}
$smarty->display($tplFile,$tplCache);
?>